package com.pd.modules.client.dialogs;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import com.pd.modules.client.database.Database;
import com.pd.modules.common.DateInterval;

@SuppressWarnings("serial")
public class DialogDateFilter extends JDialog
{
	private JFormattedTextField mTextDate;
	private JFormattedTextField mTextStartHour;
	private JFormattedTextField mTextEndHour;

	private boolean mFilterRooms;

	/**
	 * @param _applyFilterTo	true for applying filter to the rooms
	 * 							false for applying filter to the reservations
	 */
	public DialogDateFilter(boolean _applyFilterToRooms)
	{
		mFilterRooms = _applyFilterToRooms;
		
		setModal(true);
		setModalityType(ModalityType.APPLICATION_MODAL);
		if(mFilterRooms)
			setTitle("Filtrar por disponibilidade");
		else
			setTitle("Filtrar por intervalo");
		
		setBounds(100, 100, 363, 142);
		getContentPane().setLayout(new BorderLayout());
		{
			JPanel panel = new JPanel();
			panel.setLayout(null);
			getContentPane().add(panel, BorderLayout.CENTER);
			{
				JLabel label = new JLabel("Hora de Fim:");
				label.setBounds(15, 80, 78, 14);
				panel.add(label);
			}
			{
				mTextStartHour = new JFormattedTextField("HH:mm");
				mTextStartHour.setHorizontalAlignment(SwingConstants.CENTER);
				mTextStartHour.setBounds(103, 46, 103, 20);
				mTextStartHour.setValue("08:30");
				panel.add(mTextStartHour);
			}
			{
				JLabel label = new JLabel("Data:");
				label.setBounds(15, 20, 36, 14);
				panel.add(label);
			}
			{
				JLabel label = new JLabel("Hora de Inicio:");
				label.setBounds(15, 48, 86, 14);
				panel.add(label);

				mTextEndHour = new JFormattedTextField("HH:mm");
				mTextEndHour.setHorizontalAlignment(SwingConstants.CENTER);
				mTextEndHour.setBounds(103, 77, 103, 20);
				mTextEndHour.setValue("23:30");
				panel.add(mTextEndHour);

				mTextDate = new JFormattedTextField(new SimpleDateFormat("dd/MM/yyyy"));
				mTextDate.setHorizontalAlignment(SwingConstants.CENTER);
				mTextDate.setBounds(104, 17, 102, 20);
				mTextDate.setValue(Calendar.getInstance().getTime());
				panel.add(mTextDate);
			}
			{
				JButton button = new JButton("Cancelar");
				button.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						setVisible(false);
					}
				});
				button.setActionCommand("Cancel");
				button.setBounds(253, 44, 86, 23);
				panel.add(button);
			}
			{
				JButton btnFiltrar = new JButton("Filtrar");
				btnFiltrar.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg0)
					{
						// Dados das textbox's
						String txtDay = mTextDate.getText();
						String txtStartHour = mTextStartHour.getText();
						String txtEndHour = mTextEndHour.getText();

						try
						{
							DateInterval interval = new DateInterval(txtDay, txtStartHour, txtEndHour);

							// Aplica o filtro na base de dados
							if( mFilterRooms )
								Database.getInstance().Rooms().setDateFilter(interval);
							else
								Database.getInstance().Reservations().setDateFilter(interval);
							
							// Sucesso
							setVisible(false);

						} catch (Exception e)
						{
							JOptionPane.showMessageDialog(null, e.getMessage());
						}
					}
				});
				btnFiltrar.setActionCommand("OK");
				btnFiltrar.setBounds(253, 11, 86, 23);
				panel.add(btnFiltrar);
			}
		}
	}

}
